package com.twu.biblioteca.command;

import com.twu.biblioteca.BibliotecaModel;
import com.twu.biblioteca.BibliotecaSystem;
import com.twu.biblioteca.Book;
import com.twu.biblioteca.util.InputParser;
import com.twu.biblioteca.util.Messages;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/21/13
 * Time: 3:43 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReserveCommand extends Command {

    private Scanner scanner;
    private BibliotecaSystem system;

    public ReserveCommand(BibliotecaSystem system) {
        this.system = system;
        this.scanner = system.getScanner();
    }

    @Override
    public void runCommand() {
        reserveBook();
    }

    private void reserveBook() {
        if(!system.isLoggedIn()) {
            System.out.println(Messages.Login_First);
            return;
        }

        System.out.print("Select a book to reserve by id:");

        String input = scanner.nextLine();
        int selected = InputParser.parseUserSelect(input);
        if (selected <= 0 || selected > BibliotecaModel.getBooks().size()) {
            return;
        }

        Book book = BibliotecaModel.getBooks().get(selected - 1);
        if (book.isAvailable()) {
            System.out.println(Messages.Reserved_Successfully);
            book.setAvailable(false);
        } else {
            System.out.println(Messages.Reserved_UnSuccessfully);
        }
    }

}
